package com.BraveY.Demo08;


import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        Scanner S = new Scanner(System.in);
        double num1 = Math.random() * 100;
        String str = num1 + "";
        int index = str.indexOf(".");
        String newStr = str.substring(0, index + 3);
        float newNum = Float.parseFloat(newStr);
        System.out.println(newNum);

        int[] arr={2,3,-7,-9,0};
        int[] newArr = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = Math.abs(arr[i]);
        }
        for (int i = 0; i < newArr.length; i++) {
            System.out.println(newArr[i]);
        }
        System.out.println(System.currentTimeMillis());

        System.out.println("请输入：");
        String Sc = S.nextLine();
        String[] Scar = Sc.split(" ");
        int[] newA = new int[Scar.length];
        int sum = 0;
        for (int i = 0; i < Scar.length; i++) {
            newA[i] = Integer.parseInt(Scar[i]);
            sum += newA[i];
        }
        System.out.println("和："+ sum);
        for (int i = 0; i < newA.length; i++) {
            System.out.println(newA[i]);
        }

        BigDecimal num5 = new BigDecimal("10");
        BigDecimal num6 = new BigDecimal("5");
        BigDecimal num7 = num5.add(num6);
        BigDecimal num8 = num5.multiply(num6);
        System.out.println(num7);
        System.out.println(num8);

        int [] arr9 = {1,2,3,4,5,6,7,8,9,10};
        int number = 8;

        //1,我现在要干嘛? --- 二分查找
        //2.我干这件事情需要什么? --- 数组 元素
        //3,我干完了,要不要把结果返回调用者 --- 把索引返回给调用者
        int index9 = binarySearchForIndex(arr9,number);
        System.out.println("二分查找："+index9);

        int [] arr8 = {1,2,3,4,5,6,7,8,9,10};
        int index8 = Arrays.binarySearch(arr8,8);
        System.out.println(index8);
        System.out.println(Arrays.toString(arr8));


    }

    private static int binarySearchForIndex(int[] arr, int number) {
        int min = 0;
        int max = arr.length-1;
        while (min<max){
            int mid = (min + max)/2;
            if(arr[mid]>number){
                min = max-1;
            }else if (arr[mid]<number){
                min = mid +1;
            }else{
                return mid;
            }
        }
        return -1;
    }
}
